package tripx.siteget.start;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class UpdateConfig {

	private static UpdateConfig config;

	private Document configDoc;

	private UpdateConfig() {
		try {
			configDoc = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder().parse(new File("./config/setup.xml"));
		} catch (SAXException | IOException | ParserConfigurationException e) {
			System.err
					.println("When reading configuration file, error happens...");
			System.exit(-1);
		}
	}

	public static UpdateConfig getInstance() {
		if (config == null) {
			config = new UpdateConfig();
		}
		return config;
	}

	public String getServerAddr() {
		Element root = configDoc.getDocumentElement();
		Element serverEle = (Element) root.getElementsByTagName("server").item(
				0);
		return serverEle.getAttribute("address") + ":"
				+ serverEle.getAttribute("port");
	}
}
